package vsvm.gui.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import vsvm.gui.Environment;
import vsvm.gui.IUpdatable;

public class ViewMenu extends JMenu implements IUpdatable {
	
	private final static String MV_TBAR = "tbar";
	private final static String MV_SBAR = "sbar";
	private final static String MV_COL = "col";
	private final static String MV_EXP = "exp";
	private final static String MV_COL2 = "col2";
	private final static String MV_EXP2 = "exp2";
	
	private JCheckBoxMenuItem miTBar = null;
	private JCheckBoxMenuItem miSBar = null;
	private JSeparator sep = null;
	private JMenuItem miColapse = null;
	private JMenuItem miExpand = null;
	private JMenuItem miColapse2 = null;
	private JMenuItem miExpand2 = null;
	
	
	private class ViewMenuActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(MV_TBAR)) {
				Environment.getEnvironment().getMainFrame().setTBarVisible(miTBar.isSelected());
				return;
			}
			
			if (e.getActionCommand().equals(MV_SBAR)) {
				Environment.getEnvironment().getMainFrame().setSBarVisible(miSBar.isSelected());
				return;
			}
			
			if (e.getActionCommand().equals(MV_COL)) {
				Environment.getEnvironment().getMainFrame().getObjectsTree().colapse();
				return;
			}
			
			if (e.getActionCommand().equals(MV_EXP)) {
				Environment.getEnvironment().getMainFrame().getObjectsTree().expand();
				return;
			}
			
			if (e.getActionCommand().equals(MV_COL2)) {
				Environment.getEnvironment().getMainFrame().getObjectsTree().colapse(2);
				return;
			}
			
			if (e.getActionCommand().equals(MV_EXP2)) {
				Environment.getEnvironment().getMainFrame().getObjectsTree().expand(2);
				return;
			}
		}		
	}
	
	public ViewMenu() {
		super();
		this.setText("View");
		createItems();		
	}
	
	private void createItems() {
		ActionListener ml = new ViewMenuActionListener();
		
		miTBar = new JCheckBoxMenuItem("Show toolbar");
		miTBar.setActionCommand(MV_TBAR);
		miTBar.addActionListener(ml);
		
		miSBar = new JCheckBoxMenuItem("Show statusbar");
		miSBar.setActionCommand(MV_SBAR);
		miSBar.addActionListener(ml);
		
		miColapse = new JMenuItem("Colapse tree");
		miColapse.setActionCommand(MV_COL);
		miColapse.addActionListener(ml);
		
		miExpand = new JMenuItem("Expand tree");
		miExpand.setActionCommand(MV_EXP);
		miExpand.addActionListener(ml);
		
		miColapse2 = new JMenuItem("Colapse tree to 2nd level");
		miColapse2.setActionCommand(MV_COL2);
		miColapse2.addActionListener(ml);
		
		miExpand2 = new JMenuItem("Expand tree to 2nd level");
		miExpand2.setActionCommand(MV_EXP2);
		miExpand2.addActionListener(ml);
		
		
		//this.add(miTBar);
		this.add(miSBar);
		this.add(sep = new JSeparator());
		this.add(miColapse);
		this.add(miExpand);
		this.add(miColapse2);
		this.add(miExpand2);
	}
	
	public void update() {
		miSBar.setSelected(Environment.getEnvironment().getMainFrame().isSBarVisible());
		miTBar.setSelected(Environment.getEnvironment().getMainFrame().isTBarVisible());
	}

}
